package architecture.view;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.*;

import javax.swing.JMenu;

import db.*;

import architecture.reports.*;

import sf.model.Context;
import sf.view.*;

public class MenuActionAdapter implements ActionListener
{
	MainForm mainForm;
	
	// default constructor
	public MenuActionAdapter(MainForm mainForm)
	{		
		this.mainForm = mainForm;
	}	
	
	public void actionPerformed(ActionEvent e)
	{
		//System.out.println(e.getActionCommand());
		if (e.getActionCommand().equals("Log in"))
			{
				mainForm.getLoginDialog().setVisible(true);				
//				mainForm.logInRandomUser();
			}
		
		if (e.getActionCommand().equals("Log out"))
		{
			mainForm.logInAsGuest();			
		}
		
		if (e.getActionCommand().equals("Settings"))
		{
			new SettingsDialog(mainForm);		
		}
		
		if (e.getActionCommand().equals("Exit"))
		{
			mainForm.database.disconnect();
			System.exit(0);								
		}
	
	
		if (e.getActionCommand().startsWith("Tabela ")) 
		{
			String table = e.getActionCommand().substring(7);
			StandardFormFactory.createAndShow(table, mainForm.getMainForm());
		}
		
		if (e.getActionCommand().startsWith("Izvestaj ")) 
		{
			ebDBDataSet ds = new ebDBDataSet();
			String table = e.getActionCommand().substring(9).toUpperCase();			
			ds.setTableName(table);
			ds.Prepare();
			ds.Open();

			if (table.equals("IMA_PRAVO"))
				Reporting.createReport(e.getActionCommand().substring(9).toUpperCase(),ds,"reports/"+ e.getActionCommand().substring(9)+".pdf", Reporting.HIERARCHY_REPORT_TYPE);
			else Reporting.createReport(e.getActionCommand().substring(9).toUpperCase(),ds,"reports/"+ e.getActionCommand().substring(9)+".pdf", Reporting.SIMPLE_REPORT_TYPE);
		}	
		
		if (e.getActionCommand().equals("Roba i usluge")) 
		{
			StandardFormFactory.createAndShow("ROBA_I_USLUGE", mainForm.getMainForm());
		}

		if (e.getActionCommand().equals("Cenovnik")) 
		{
			StandardFormFactory.createAndShow("CENOVNIK", mainForm.getMainForm());
		}

		if (e.getActionCommand().equals("Otvori korpu")) 
		{
			StandardFormFactory.createAndShow("POSLOVNE_TRANSAKCIJE", mainForm.getMainForm());
		}
		
		if (e.getActionCommand().startsWith("Stampaj korpu")) 
		{
			ebDBDataSet ds = new ebDBDataSet();
			ds.setTableName("POSLOVNA_TRANSAKCIJA");
			ds.Prepare();
			ds.Open();
			Reporting.createReport("POSLOVNE_TRANSAKCIJE",ds,"reports/"+ "POSLOVNA_TRANSAKCIJA"+".pdf", Reporting.HIERARCHY_REPORT_TYPE);
		}	

		if (e.getActionCommand().equals("Cene po jedinici mere")) 
		{
			StandardFormFactory.createAndShow("CENE_PO_JEDINICI_MERE", mainForm.getMainForm());
		}
		
		if (e.getActionCommand().equals("Robe i usluge")) 
		{
			StandardFormFactory.createAndShow("ROBA_I_USLUGE", mainForm.getMainForm());
		}
		
		
		if (e.getActionCommand().equals("Poslovna godina")) 
		{
			StandardFormFactory.createAndShow("POSLOVNA_GODINA", mainForm.getMainForm());
		}
		
		if (e.getActionCommand().equals("Poslovni odnos")) 
		{
			StandardFormFactory.createAndShow("POSLOVNI_ODNOS", mainForm.getMainForm());
		}
		
		if (e.getActionCommand().equals("Poslovne transakcije")) 
		{
			StandardFormFactory.createAndShow("POSLOVNE_TRANSAKCIJE", mainForm.getMainForm());
		}
		
		if (e.getActionCommand().equals("Sektori")) 
		{
			StandardFormFactory.createAndShow("SEKTORI", mainForm.getMainForm());
		}
		
		if (e.getActionCommand().equals("Kursna lista")) 
		{
			StandardFormFactory.createAndShow(new Context(null, StandardForm.MODE_SEARCH | StandardForm.FROM_MENU, "KURSNA_LISTA"), mainForm.getMainForm());
		}

	}

}
